﻿namespace NTLib.Test.Resources
{
    using Core.ScopeLock;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// 
    /// </summary>
    public static class TestFileHelper
    {
        #region Methods

        /// <summary>
        /// Creates the temporary folder an will clean the folder when teh ScopeLockItem is Disposed.
        /// </summary>
        public static ScopeLockItem<string> CreateTempFolder()
        {
            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            if (!Directory.Exists(tempPath))
                Directory.CreateDirectory(tempPath);

            return new ScopeLockItem<string>(tempPath, (dir) =>
            {
                var files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
                foreach (var f in files)
                {
                    try
                    {
                        File.Delete(f);
                    }
                    catch (Exception)
                    {

                    }
                }

                try
                {
                    Directory.Delete(dir);
                }
                catch (Exception)
                {

                }
            });
        }

        #endregion
    }
}
